package com.atguigu.jxc.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.jxc.dao.CustomerReturnListDao;
import com.atguigu.jxc.dao.CustomerReturnListGoodsDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.CustomerReturnList;
import com.atguigu.jxc.entity.CustomerReturnListGoods;
import com.atguigu.jxc.entity.Log;
import com.atguigu.jxc.entity.SaleList;
import com.atguigu.jxc.entity.query.CustomerReturnListQuery;
import com.atguigu.jxc.service.CustomerReturnListGoodsService;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Description:
 *
 * @author WJL
 * @date 2021/5/13
 */
@Service
//@Transactional
public class CustomerReturnListGoodsServiceImpl implements CustomerReturnListGoodsService {

    @Autowired
    CustomerReturnListGoodsDao customerReturnListGoodsDao;
    @Autowired
    CustomerReturnListDao customerReturnListDao;
    @Autowired
    private LogService logService;

    @Transactional
    @Override
    public ServiceVO save(String returnNumber, CustomerReturnList customerReturnList, String customerReturnListGoodsStr) {
        if (StringUtil.isEmpty(returnNumber) || StringUtil.isEmpty(customerReturnListGoodsStr) || StringUtil.isEmpty(customerReturnList.toString())) {
            return new ServiceVO<>(1001, "请求参数为空");
        }
        try {
            // 将退货单号存入客户退货单对象中
            customerReturnList.setReturnNumber(returnNumber);
            if (customerReturnList.getUserId() == null) {
                customerReturnList.setUserId(1);
            }
            // 使用fast—json 将Json字符串转换成对象
            List<CustomerReturnListGoods> customerReturnListGoods = JSON.parseArray(customerReturnListGoodsStr, CustomerReturnListGoods.class);
            // 保存客户退货单
            customerReturnListDao.insert(customerReturnList);
            customerReturnListGoods.forEach(customerReturnListGood -> {
                customerReturnListGood.setCustomerReturnListId(customerReturnList.getCustomerReturnListId());
                customerReturnListGoodsDao.insert(customerReturnListGood);
            });

            return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new ServiceVO<>(403,"数据库插入失败");
        }
    }

    @Override
    public Map<String, Object> list(CustomerReturnListQuery query) {
        HashMap<String, Object> result = new HashMap<>();
        try {
            List<CustomerReturnList> customerReturnLists = customerReturnListDao.getCustomerReturnList(query);
            logService.save(new Log(Log.SELECT_ACTION, "客户退货单查询"));
            result.put("rows", customerReturnLists);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public Map<String, Object> queryGoodsList(Integer customerReturnListId) {
        HashMap<String, Object> result = new HashMap<>();
        try {
            List<CustomerReturnListGoods> customerReturnListGoods = customerReturnListGoodsDao.queryGoodsList(customerReturnListId);
            logService.save(new Log(Log.SELECT_ACTION, "客户退货单商品查询"));
            result.put("rows", customerReturnListGoods);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(result);
        return result;
    }

    @Override
    @Transactional
    public ServiceVO delete(Integer customerReturnListId) {
        try {
            customerReturnListGoodsDao.delete(customerReturnListId);
            customerReturnListDao.delete(customerReturnListId);
        } catch (Exception e) {
            e.printStackTrace();
            return new ServiceVO<>(ErrorCode.CONSTRAINT_VIOLATION_CODE, ErrorCode.CONSTRAINT_VIOLATION_MESS);
        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }
}
